package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;


    //发布动态
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    //查询我的动态
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pagesize,
                             Long userId) {
        PageResult pr = movementService.findMyAll(page, pagesize, userId);

        return ResponseEntity.ok(pr);
    }

    //查询好友动态
    @GetMapping
    public ResponseEntity findFriendMovements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pr = movementService.findFriendMovements(page, pagesize);

        return ResponseEntity.ok(pr);
    }

    //查询推荐动态
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pr = movementService.findRecommendMovements(page, pagesize);

        return ResponseEntity.ok(pr);
    }


    //根据id查询单条动态详情
    @GetMapping("/{id}")
    public ResponseEntity findRecommendById(@PathVariable("id") String movementId){
        //调用service查询
       MovementsVo vo= movementService.findRecommendById(movementId);
       return ResponseEntity.ok(vo);
    }


    /**
     * 动态-评论-点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        //点赞返回点赞总记录数
        Integer likeCount = movementService.likeMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态取消-评论-点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId){
        //取消点赞后,返回点赞总记录数
        Integer likeCount = movementService.disLikeMovement(movementId);
        return ResponseEntity.ok(likeCount);

    }

    /**
     * 喜欢动态
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        //喜欢动态后,返回动态喜欢总记录数
        Integer likeCount = movementService.loveMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消动态喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        //取消喜欢后,返回喜欢总记录数
        Integer likeCount = movementService.disloveMovement(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 查询谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
       List<VisitorsVo> list= movementService.queryVisitorsList();
       return ResponseEntity.ok(list);
    }
}

